package com.bdqn.desiremall.serivce.shoppingcart;

import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.bdqn.desiremall.entity.Mall_Goods;
import com.bdqn.desiremall.entity.Mall_ShoppingCart;
import com.bdqn.desiremall.entity.Mall_ShoppingItem;
import com.bdqn.desiremall.mapper.Mall_OrderMapper;
import com.bdqn.desiremall.mapper.Mall_ShoppingCartMapper;
import com.bdqn.desiremall.mapper.Mall_ShoppingItemMapper;
import com.bdqn.desiremall.mapper.Mall_UserMapper;
import com.desiremall.util.MyBatisUtil;

public class ShoppingcartServiceImpl implements ShoppingcartService {
	SqlSession session =null;
	
	@Override
	public List<Mall_Goods> shoppingCartView(int userId) {
		List<Mall_Goods> listCar=null;
		try {
			//1����ȡsqlSession����
			session=MyBatisUtil.getSession();
			//2����ȡUserMapperʵ��
			Mall_ShoppingCartMapper shoppingCartMapper=session.getMapper(Mall_ShoppingCartMapper.class);
			listCar= shoppingCartMapper.findShoppingCart(userId);
			}catch (Exception e) {
		//session.rollback();
		e.printStackTrace();
	}
	finally
	{
		MyBatisUtil.closeSqlSession(session);
		}
		return listCar;
	}

	@Override
	public boolean goodsRemoveFromShoppingCart(int shoppingCartId) {
		int i=0;
		try {
			//1����ȡsqlSession����
			session=MyBatisUtil.getSession();
			//2����ȡUserMapperʵ��
			Mall_ShoppingCartMapper shoppingCartMapper=session.getMapper(Mall_ShoppingCartMapper.class);
			i= shoppingCartMapper.modifyShoppingCart(shoppingCartId);
			if(0<i)
			{
				session.commit();
				return true;
			}
			}catch (Exception e) {
		session.rollback();
		e.printStackTrace();
	}
	finally
	{
		MyBatisUtil.closeSqlSession(session);
		}
		return false;
	}

	@Override
	public boolean addShoppingItem(Mall_ShoppingItem shoppingItem) {
		int i=0;
		try {
				//1����ȡsqlSession����
				session=MyBatisUtil.getSession();
				//2����ȡUserMapperʵ��
				Mall_ShoppingItemMapper shoppingItemMapper=session.getMapper(Mall_ShoppingItemMapper.class);
				i= shoppingItemMapper.addShoppingItem(shoppingItem);
				if(0<i)
				{
				session.commit();
				return true;
				}
				
			}catch (Exception e) 
		{
			session.rollback();
			e.printStackTrace();
		}
	finally{
			MyBatisUtil.closeSqlSession(session);
		}
			return false;
	}

	@Override
	public boolean addGoodsToShoppingCart(Mall_ShoppingCart shoppingCart) {
		int i=0;
		try {
				//1����ȡsqlSession����
				session=MyBatisUtil.getSession();
				//2����ȡUserMapperʵ��
				Mall_ShoppingCartMapper shoppingCartMapper=session.getMapper(Mall_ShoppingCartMapper.class);
				i= shoppingCartMapper.addGoodsToCart(shoppingCart);
				if(0<i)
				{
				session.commit();
				return true;
				}
				
			}catch (Exception e) 
		{
			session.rollback();
			e.printStackTrace();
		}
	finally{
			MyBatisUtil.closeSqlSession(session);
		}
			return false;
	}



}
